﻿$(function() {
    //加入购物车效果-门店电脑配置不高有卡顿已去掉(2016-12-2)
    //fly();
});

function addCart(productId) {
    var buyNum = $(".buy_" + productId).val();
    if (buyNum <= 0) {
        layer.alert("订购数量值无效");
        return;
    }
    var jsonStr = "[";
    jsonStr += "{";
    jsonStr += "\"PreQty\":\"" + buyNum + "\",";
    jsonStr += "\"ProductId\":\"" + productId + "\"";
    jsonStr += "}";
    jsonStr += "]";

    $("#btnBuy_" + productId).css("background", "#909090");
    //透明遮罩层
    layer.load(0);
    $(".layui-layer").hide();
    
    //加入购物车
    $.ajax({
        url: "../Order/AddCart",
        type: "post",
        data: { productsJson: jsonStr },
        success: function (msg) {
            $("#btnBuy_" + productId).css("background", "#19b1e3");
            layer.closeAll();
            if (msg == "OK") {
                layer.msg('成功加入购物车', { time: 1000 });
                
                clearCartCookie();
                loadCartCount();

                var cartQty = $("#cartQty_" + productId).text();
                $("#cartQty_" + productId).text(parseInt(cartQty) + parseInt(buyNum));
                $("#cartQty_" + productId).show();

            } else {
                layer.alert(msg);
            }
        }
    });
}


//加入购物车效果
function fly() {
    $(".buy").click(function (event) {
        var addcar = $(this);
        var img = addcar.parents("li").find('img').attr('src');
        var flyer = $('<img class="u-flyer" src="' + img + '">');
        flyer.fly({
            start: {
                left: event.pageX - 500,
                top: event.pageY - $(document).scrollTop()-140
            },
            end: {
                left: $(".cart").offset().left + 60,
                top: $(".cart").offset().top + 20 - $(document).scrollTop(),
                width: 0,
                height: 0
            },
            onEnd: function () {
                //alert("成功");
            }
        });
    });
}